
const fs = require('fs');
let manifest = fs.readFileSync('./src/manifest.json');
let manifestData = JSON.parse(manifest);

let versionName=manifestData.versionName;
let versionCode=manifestData.versionCode;
let versionLog="";
let count=0;
let isFirst=false; //是否版本第一个

let versionCount=fs.readFileSync('./versionCount.json');
let versionCountData=JSON.parse(versionCount);
if(versionCountData[versionName]){
  count=versionCountData[versionName]=versionCountData[versionName]+1;
  versionLog=versionCountData[versionName].log;
}else{
  count=versionCountData[versionName]=1;
  isFirst=true;
}
fs.writeFileSync('./versionCount.json',JSON.stringify(versionCountData));

let configData=fs.readFileSync('./src/config/index.js','utf-8');
let versionStart=configData.indexOf("app_version_count");
let versionCountEnd=configData.indexOf("//versionCountEnd");
let versionCountStr=configData.substring(versionStart,versionCountEnd);
configData=configData.replace(versionCountStr,`app_version_count:${count},`);

let appVersionStart=configData.indexOf("app_version");
let versionEnd=configData.indexOf("//versionEnd");
let versionStr=configData.substring(appVersionStart,versionEnd);
configData=configData.replace(versionStr,`app_version:'${versionName}',`);


fs.writeFileSync('./src/config/index.js',configData);

let buildType =process.argv[2];
console.log("buildType:",buildType)
const path = require("path");
const processCommand = require("child_process");
const headCommand = process.platform == "darwin" ? "sh " : "";
processCommand.exec(
  headCommand +
    path.join(__dirname, "./deploy.sh") +
    ` ${versionName} ${versionCode} ${count} ${versionLog} ${buildType} `,
  function(error, stdout) {
    if (error !== null) {
      console.log("exec error: " + error);
    }
    console.log(stdout);
  }
);


